package com.cema.manage.project.manage.proGoods.controller;
import java.util.ArrayList;
import java.util.List;
import com.cema.manage.common.utils.StringUtils;
import com.cema.manage.project.manage.proGoods.domain.ProGoods;
import com.cema.manage.project.manage.proGoods.service.IProGoodsService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.domain.Message;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cema.manage.framework.web.page.TableDataInfo;


/**
 * 内容管理 信息操作处理
 *
 * @author reasahi
 * @date 2024-10-13
 */
@Controller
@RequestMapping("/manage/proGoods")
public class ProGoodsController extends BaseController {
    private String prefix = "manage/proGoods";

    @Autowired
    private IProGoodsService proGoodsService;

    @GetMapping()
    @RequiresPermissions("manage:proGoods:view")
    public String proGoods() {
        return prefix + "/proGoods";
    }

    /**
     * 查询内容管理列表
     */
    @RequiresPermissions("manage:proGoods:list")
    @ResponseBody
    @GetMapping("/list")
    public TableDataInfo list(ProGoods proGoods) {
        startPage();
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(proGoods);
        List<ProGoods> list = proGoodsService.selectList(entityWrapper);
        return getDataTable(list);
    }

    /**
     * 新增内容管理
     */
    @RequiresPermissions("manage:proGoods:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 修改内容管理
     */
    @RequiresPermissions("manage:proGoods:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        ProGoods proGoods =proGoodsService.selectById(id);
        model.addAttribute("proGoods", proGoods);
        return prefix + "/edit";
    }

    /**
     * 保存内容管理
     */
    @RequiresPermissions("manage:proGoods:save")
    @PostMapping("/save")
    @ResponseBody
    public Message save(ProGoods proGoods) {
        Integer id = proGoods.getId();
        if (StringUtils.isNotNull(id)) {
            if (proGoodsService.updateById(proGoods)) {
                return Message.success();
            }
            return Message.error();
        } else {
            if (proGoodsService.insert(proGoods)) {
                return Message.success();
            }
            return Message.error();
        }
    }

    /**
     * 删除内容管理
     */
    @RequiresPermissions("manage:proGoods:remove")
    @PostMapping("/remove/{id}")
    @ResponseBody
    public Message remove(@PathVariable("id") Integer id) {
        if (proGoodsService.deleteById(id)) {
            return Message.success();
        }
        return Message.error();
    }

    /**
     * 批量删除内容管理
     */
    @RequiresPermissions("manage:proGoods:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public Message remove(@RequestParam("ids[]") Integer[]ids) {
        List list = new ArrayList();
        for (Integer n:ids) {
            list.add(n);
        }
        if (proGoodsService.deleteBatchIds(list)) {
            return Message.success();
        }
        return Message.error();
    }

}
